#!/usr/bin/env python3.8
# -*- encoding: utf-8 -*-
'''
@File    :   decorators.py
@Time    :   2021/10/20 16:33:04
@Author  :   windstarry 
@Version :   1.0
'''
# here put the import lib
from functools import wraps

from flask import Markup, flash, url_for, redirect, abort
from flask_login import current_user

#确认用户是否验证装饰器
def confirm_required(func):
    @wraps(func)
    def decorated_function(*args, **kwargs):
        if not current_user.confirmed:
            message = Markup(
                '请通过邮箱验证账号.'
                '没收到邮件?'
                '<a class="alert-link" href="%s">重新发送邮件</a>' %
                url_for('auth.resend_confirm_email'))
            flash(message, 'warning')
            return redirect(url_for('main.index'))
        return func(*args, **kwargs)
    return decorated_function


def permission_required(permission_name):
    def decorator(func):
        @wraps(func)
        def decorated_function(*args, **kwargs):
            if not current_user.can(permission_name):
                abort(403)
            return func(*args, **kwargs)
        return decorated_function
    return decorator


def admin_required(func):
    return permission_required('ADMINISTER')(func)